<template>
  
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="报修人名称" prop="repairPeoplename">
				<el-input v-model="queryParams.repairPeoplename" placeholder="请输入报修人名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="报修人电话" prop="repairPeoplephone">
				<el-input v-model="queryParams.repairPeoplephone" placeholder="请输入报修人电话" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="报修状态" prop="repairStatus">
				<el-select v-model="queryParams.repairStatus" placeholder="请选择报修状态" clearable>
					<el-option v-for="dict in dict.type.sys_baoxiu_status" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

    
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['rcgl:repair:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['rcgl:repair:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['rcgl:repair:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['rcgl:repair:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

    
    <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="repairId" />
      <el-table-column label="报修人名称" align="center" prop="repairPeoplename" />
      <el-table-column label="报修人电话" align="center" prop="repairPeoplephone" />
      <el-table-column label="报修地址" align="center" prop="repairAddress" />
      <el-table-column label="报修状态" align="center" prop="repairStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_baoxiu_status" :value="scope.row.repairStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="报修描述" align="center" prop="repairDescription" />
      <el-table-column label="报修图片" align="center" prop="repairImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.repairImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="报修预约时间" align="center" prop="repairAppointmenttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairAppointmenttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报修维修时间" align="center" prop="repairMaintenancetime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
      
          <el-button v-if="scope.row.repairStatus=='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rcgl:repair:edit']" 
          >修改</el-button>
          <!-- 设置一个维修的按钮打开维修的对话框 -->
           <el-button v-if="scope.row.repairStatus=='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlerepair(scope.row)"
            v-hasPermi="['rcgl:repair:edit']"
          >维修</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rcgl:repair:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    
    <!-- 维修的对话框 -->
     <el-dialog :title="title" :visible.sync="repair" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报修人名称" prop="repairPeoplename">
          <el-input v-model="form.repairPeoplename" placeholder="请输入报修人名称" />
        </el-form-item>
        <el-form-item label="报修人电话" prop="repairPeoplephone">
          <el-input v-model="form.repairPeoplephone" placeholder="请输入报修人电话" />
        </el-form-item>
        <el-form-item label="报修地址" prop="repairAddress">
          <el-input v-model="form.repairAddress" placeholder="请输入报修地址" />
        </el-form-item>
				<el-form-item label="报修问题" prop="repairDescription">
					<el-input v-model="form.repairDescription" placeholder="请输入报修描述" />
				</el-form-item>
				<!-- 指派维修员 下拉框选择维修员-->
				<el-form-item label="选择维修员" prop="fixmanId">
					<el-select v-model="form.fixmanId" placeholder="请选择维修员">
						<el-option v-for="dict in fixmans" :key="dict.fixmanId" :label="dict.fixmanName"
							:value="parseInt(dict.fixmanId)" />
					</el-select>
				</el-form-item>
			</el-form>

			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>


 


		<!-- 添加或修改报修处理对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="报修人名称" prop="repairPeoplename">
					<el-input v-model="form.repairPeoplename" placeholder="请输入报修人名称" />
				</el-form-item>
				<el-form-item label="报修人电话" prop="repairPeoplephone">
					<el-input v-model="form.repairPeoplephone" placeholder="请输入报修人电话" />
				</el-form-item>


       


				<el-form-item label="小区" prop="repairAddress">
					<el-select v-model="queryWbu.communityId" placeholder="请选择小区" @keyup.enter.native="handleQuery">
						
						<el-option v-for="item in communityList" :label="item.communityName" :value="item.communityId"
							:key="item.communityId">
						</el-option>
					</el-select>
				</el-form-item>
				
				<el-form-item label="楼座" prop="repairAddress">
					<el-select v-model="queryWbu.balconyId" placeholder="请选择楼座" @keyup.enter.native="handleQuery">
						<el-option v-for="item in balconyList" :key="item.balconyId" :label="item.balconyName"
							:value="item.balconyId">
						</el-option>
					</el-select>
				</el-form-item>
				
				<el-form-item label="单元" prop="repairAddress">
					<el-select v-model="queryWbu.unitId" placeholder="请选择单元" @keyup.enter.native="handleQuery">
						<el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName"
							:value="item.unitId">
						</el-option>
					</el-select>
				</el-form-item>
				
				<el-form-item label="房号" prop="repairAddress">
					<el-select v-model="queryWbu.homeId" placeholder="请选择房号" @keyup.enter.native="handleQuery">
						<el-option v-for="item in homeList" :key="item.homeId" :label="item.homeNumber"
							:value="item.homeId">
						</el-option>
					</el-select>
				</el-form-item>




        
				<el-form-item label="报修状态" prop="repairStatus">
					<el-select v-model="form.repairStatus" placeholder="请选择报修状态">
						<el-option v-for="dict in dict.type.sys_baoxiu_status" :key="dict.value" :label="dict.label"
							:value="parseInt(dict.value)"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="报修描述" prop="repairDescription">
					<el-input v-model="form.repairDescription" placeholder="请输入报修描述" />
				</el-form-item>
				<el-form-item label="报修图片" prop="repairImage">
					<image-upload v-model="form.repairImage" />
				</el-form-item>
				<el-form-item label="报修预约时间" prop="repairAppointmenttime">
					<el-date-picker clearable v-model="form.repairAppointmenttime" type="date" value-format="yyyy-MM-dd"
					:picker-options="pickerOptions" placeholder="请选择报修预约时间">
					</el-date-picker>
				</el-form-item>
				<!-- <el-form-item label="报修维修时间" prop="repairMaintenancetime">
          <el-input v-model="form.repairMaintenancetime" placeholder="请输入报修维修时间" />
        </el-form-item> -->

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>

import { listRepair, getRepair, delRepair, addRepair, updateRepair,editRepair } from "@/api/rcgl/repair";
import { listFixmanAll } from "@/api/rcgl/fixman";
import { getCommunity,getBalconyUnitHome } from "@/api/charge/allocation";
export default {
  name: "Repair",
  dicts: ['sys_baoxiu_status'],
  data() {
    return {
      //维修员集合
      fixmans: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修处理表格数据
      repairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,


				// 是否显示维修对话框
				repair: false,
				//定义一个状态值来表示是修改 还是维修 还是添加
				status: '',
				//
				// 小区列表数据
				communityList: [],
				// 楼座列表数据
				balconyList: [],
				// 单元列表数据
				unitList: [],
				// 房间列表数据
				homeList: [],
				queryWbu: {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				},

   
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					repairPeoplename: null,
					repairPeoplephone: null,
					repairStatus: null,
				},
				// 表单参数
				form: {
					repairAddress: null,
					repairAppointmenttime: '', 
				},
				pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7; // 禁用今天之前的日期
        }
      },
				// 表单校验
				rules: {
					repairAddress: [{
						required: true,
						message: '请选择位置信息',
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			this.getList();
			this.getFixmans();
			this.getCommunityList();
		},
		methods: {
			// 获取小区集合 调用查询所有小区的接口 获得所有小区
			getFixmans() {
				this.loading = true;
				listFixmanAll().then(response => {
					this.fixmans = response.rows;
					console.log(this.fixmans)
					this.loading = false;
				});
			},


   
			/** 查询报修处理列表 */
			getList() {
				this.loading = true;
				listRepair(this.queryParams).then(response => {
					this.repairList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				//关闭维修对话框
				this.repair = false;
				this.queryWbu = {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				};
    
				queryWbu = {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				};
				this.reset();


			},
			// 表单重置
			reset() {
				this.form = {
					repairId: null,
					repairPeoplename: null,
					repairPeoplephone: null,
					repairAddress: null,
					repairStatus: null,
					repairDescription: null,
					repairImage: null,
					repairAppointmenttime: null,
					repairMaintenancetime: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.repairId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.status = 0
				this.open = true;
				this.title = "添加报修处理";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				//设置一个状态值来表示选择是修改
				this.status = 1
				const repairId = row.repairId || this.ids
				getRepair(repairId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改报修处理";
				});
			},
			handlerepair(row) {
				this.reset();
				this.status = 2
				const repairId = row.repairId || this.ids
				getRepair(repairId).then(response => {
					this.form = response.data;
					this.repair = true;
					this.title = "维修处理";
				});
			},
			/** 提交按钮 */
			submitForm() {
				console.log(this.form.repairAddress)
				this.$refs["form"].validate(valid => {
					if (valid) {
						//
						if (this.form.repairId != null && this.status == 1) {
							updateRepair(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else if (this.form.repairId != null && this.status == 2) {
							editRepair(this.form).then(response => {
								this.$modal.msgSuccess("维修成功");
								this.repair = false;
								this.getList();
							});
						} else {
							addRepair(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const repairIds = row.repairId || this.ids;
				this.$modal.confirm('是否确认删除报修处理编号为"' + repairIds + '"的数据项？').then(function() {
					return delRepair(repairIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('rcgl/repair/export', {
					...this.queryParams
				}, `repair_${new Date().getTime()}.xlsx`)
			},

			/* 获取小区列表 */
			getCommunityList() {
				getCommunity().then(res => {
					this.communityList = res.data
				})
			},


		},
		watch: {
			'queryWbu.communityId': {
				handler() {

					this.balconyList = []
					this.unitList = []
					this.homeList = []
					this.queryWbu.balconyId = null
					this.queryWbu.unitId = null
					this.queryWbu.homeId = null

					this.form.repairAddress = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.balconyList = res.data[0]
         

						// if (res.data.length > 3) {
						// 	this.queryParams.balconyunithomeIds = res.data[3]
						// 	this.form.balconyunithomeIds = res.data[3]
						// }
						// this.queryParams.communicateId = this.queryWbu.communityId
						// this.form.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.balconyId': {
				handler() {


					this.unitList = []
					this.homeList = []
					this.queryWbu.unitId = null
					this.queryWbu.homeId = null


					this.form.repairAddress = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.unitList = res.data[1]
						// if (res.data.length > 3) {
						// 	this.queryParams.balconyunithomeIds = res.data[3]
						// 	this.form.balconyunithomeIds = res.data[3]
						// }
						// this.queryParams.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.unitId': {
				handler() {

					this.homeList = []
					this.queryWbu.homeId = null


					this.form.repairAddress = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.homeList = res.data[2]
						// if (res.data.length > 3) {
						// 	this.queryParams.balconyunithomeIds = res.data[3]
						// 	this.form.balconyunithomeIds = res.data[3]
						// }
						// this.queryParams.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.homeId': {
				handler(newId) {
					this.form.repairAddress = null
					if (newId) {
						getBalconyUnitHome(this.queryWbu).then(res => {
							getCommunity(this.queryWbu).then(response => {
								this.form.repairAddress = response.data[0].communityName + res.data[0][0]
									.balconyName + res.data[1][0].unitName + res.data[2][0].homeNumber +
									"房间"
								console.log(this.form.repairAddress)
							})
						})
					}
				}
			},
			deep: true,
			immediate: true
		}

	};
</script>